package com.tansun.easycare.rule.ml.algorithm.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.ml.algorithm.entity.ApplyData;
import com.tansun.easycare.rule.ml.algorithm.entity.ImgVariables;
import com.tansun.easycare.rule.ml.algorithm.entity.PyResponse;
import com.tansun.easycare.rule.ml.algorithm.service.IAlgorthmService;
import com.tansun.easycare.rule.ml.model.entity.MlDataModel;
import com.tansun.easycare.rule.ml.model.service.MlDataModelService;


/**
 * 模型应用控制类
 * @author xch
 * @version 2018-08-24
 */
@Controller
@RequestMapping(value = "/ml/apply")
public class ModelApplyController extends BaseController{
	
	@Autowired
	private MlDataModelService mlDataModelService;

	@Autowired
	private IAlgorthmService iAlgrthmService;
	
	@ModelAttribute
	public MlDataModel get(@RequestParam(required=false) String id) {
		MlDataModel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mlDataModelService.get(id);
		}
		if (entity == null){
			entity = new MlDataModel();
		}
		return entity;
	}
	
	@RequestMapping(value = "modelApplySingle")
	public @ResponseBody JSONObject modelApplySingle(MlDataModel mlDataModel,ApplyData applyData,ImgVariables imgVariables,RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.modelApply(mlDataModel, applyData, imgVariables);
		JSONObject applyResult = (JSONObject)resp.getResponse().get("modelApply");
		addMessage(redirectAttributes, "获取单样本应用成功");
		return applyResult;
	}
	

	@RequestMapping(value = "modelApplyByFile")
	public @ResponseBody JSONObject modelApplyByFile(MlDataModel mlDataModel,ApplyData applyData,ImgVariables imgVariables, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.modelApply(mlDataModel, applyData, imgVariables);
		JSONObject applyResult = (JSONObject)resp.getResponse().get("modelApply");
		addMessage(redirectAttributes, "获取测试数据文件应用成功");
		return applyResult;
	}
	
	
	@RequestMapping(value = "dataSetProcess")
	public @ResponseBody JSONObject dataSetProcess(MlDataModel mlDataModel,String dataSetFile, RedirectAttributes redirectAttributes, Model model) {
		PyResponse resp = iAlgrthmService.dataSetProcess(mlDataModel, dataSetFile);
		JSONObject applyResult = (JSONObject)resp.getResponse().get("dataSetProcess");
		addMessage(redirectAttributes, "获取测试数据文件应用成功");
		return applyResult;
	}
	
	
	
	@RequestMapping(value = "className")
	public @ResponseBody JSONObject className(MlDataModel mlDataModel, ApplyData applyData, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.className(mlDataModel,applyData);
		JSONObject className = (JSONObject)resp.getResponse().get("className");
		addMessage(redirectAttributes, "聚类重命名成功");
		return className;
	}
	
	@RequestMapping(value = "getClass")
	public @ResponseBody JSONObject getClass(MlDataModel mlDataModel, RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.getClass(mlDataModel);
		JSONObject getClass = (JSONObject)resp.getResponse().get("getClass");
		addMessage(redirectAttributes, "获取类别数据成功");
		return getClass;
	}
	
	@RequestMapping(value = "setVaribleOperate")
	public @ResponseBody JSONObject setVaribleOperate(MlDataModel mlDataModel,ImgVariables imgVariables,RedirectAttributes redirectAttributes) {
		PyResponse resp = iAlgrthmService.setVaribleOperate(mlDataModel,imgVariables);
		JSONObject jsonobj = (JSONObject)resp.getResponse().get("setVaribleOperate");
		addMessage(redirectAttributes, "获取类别数据成功");
		return jsonobj;
	}
	
	
}
